package com.novoda.runbuddy.provider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;

import com.novoda.runbuddy.R;
import com.novoda.runbuddy.util.DBUtil;
import com.novoda.runbuddy.util.SqlScanner;

public class ContentProviderCheckPointTest extends AndroidTestCase {
	private static final String	RUN_BUDDY_DB		= "runBuddyDB1.db";
	private static final long	CENTER_LONGITUDE	= -4233360;
	private static final long	CENTER_LATITUDE		= 55849004;
	private static final long	ALTITUDE			= 1002;

	// Declaring all the URIs against which we are going to do some tests.
	private static final Uri	ALL_CHECKPOINTS		= Uri.parse("content://com.novoda.runbuddy/checkpoints");
	private static final Uri	FIRST_CHECKPOINT	= Uri.parse("content://com.novoda.runbuddy/checkpoints/1");
	private SQLiteDatabase		mdb;

	private DBUtil				dbUtil				= null;

	protected void setUp() {
		if (dbUtil == null) {
			dbUtil = new DBUtil(getContext(), RUN_BUDDY_DB);
		}
		// insert initial values
		dbUtil.executeDB(R.raw.positron_cp_route_insert);
		mdb = dbUtil.getDatabase();
	}

	@Override
	protected void tearDown() throws Exception {
		if (dbUtil == null) {
			dbUtil = new DBUtil(getContext(), RUN_BUDDY_DB);
		}
		dbUtil.executeDB(R.raw.db_delete_all_values);
		mdb.close();
		super.tearDown();
	}

	public void testGettingAllCheckPoints() throws Exception {
		Cursor cur = getContext().getContentResolver().query(ALL_CHECKPOINTS, new String[] { "name" }, null, null, null);
		cur.moveToLast();
		String actual = cur.getString(0);
		assertEquals("ch4", actual);
		cur.close();
	}

	public void testInsertingNewCheckPoint() throws Exception {
		ContentValues values = new ContentValues();
		values.put(RunBuddyCP.CheckPoints.ALTITUDE, ALTITUDE);
		values.put(RunBuddyCP.CheckPoints.LATITUDE, CENTER_LATITUDE);
		values.put(RunBuddyCP.CheckPoints.LONGITUDE, CENTER_LONGITUDE);

		Uri uri = getContext().getContentResolver().insert(ALL_CHECKPOINTS, values);
		assertEquals(Uri.parse("content://com.novoda.runbuddy/checkpoints/5"), uri);

		Cursor cur = getContext().getContentResolver().query(uri, new String[] { "latitude", "longitude" }, null, null, null);
		assertNotNull(cur);
		cur.moveToFirst();
		assertEquals(CENTER_LATITUDE, cur.getLong(0));
		assertEquals(CENTER_LONGITUDE, cur.getLong(1));
		cur.close();
	}

	public void testGetFirstCheckpoint() throws Exception {
		Cursor cur = getContext().getContentResolver().query(FIRST_CHECKPOINT, new String[] { "name" }, null, null, null);
		assertNotNull(cur);
		cur.moveToFirst();
		String expected = "ch1";
		assertEquals(expected, cur.getString(0));
		cur.close();
	}

	public void testGettingTheLatitude() throws Exception {
		Cursor cur = getContext().getContentResolver().query(FIRST_CHECKPOINT, new String[] { "latitude", "longitude" }, null,
				null, null);
		assertNotNull(cur);
		cur.moveToFirst();
		long lat = 55846809;
		long lon = -4229920;
		assertEquals(lat, cur.getLong(0));
		assertEquals(lon, cur.getLong(1));
		cur.close();
	}

	public void testDeletingASingleCheckPoint() throws Exception {
		assertEquals(1, getContext().getContentResolver().delete(FIRST_CHECKPOINT, null, null));
	}
}
